/*
 * This file is part of HE_Mesh, a library for creating and manipulating meshes.
 * It is dedicated to the public domain. To the extent possible under law,
 * I , Frederik Vanhoutte, have waived all copyright and related or neighboring
 * rights.
 * 
 * This work is published from Belgium. (http://creativecommons.org/publicdomain/zero/1.0/)
 * 
 */
package wblut.hemesh;

import java.util.Iterator;
import wblut.geom.WB_GeometryFactory;
import wblut.geom.WB_Point;
import wblut.geom.WB_Sphere;

/**
 * 
 */
public class HEM_Spherify extends HEM_Modifier {
    
    /**
     * 
     */
    private final WB_Sphere sphere;
    
    /**
     * 
     */
    private double factor;
    
    /**
     * 
     */
    private final WB_GeometryFactory gf = WB_GeometryFactory.instance();

    /**
     * 
     */
    public HEM_Spherify() {
	super();
	sphere = new WB_Sphere();
	factor = 1.0;
    }

    /**
     * 
     *
     * @param r 
     * @return 
     */
    public HEM_Spherify setRadius(final double r) {
	sphere.setRadius(r);
	return this;
    }

    /**
     * 
     *
     * @param x 
     * @param y 
     * @param z 
     * @return 
     */
    public HEM_Spherify setCenter(final double x, final double y, final double z) {
	sphere.getCenter().set(x, y, z);
	return this;
    }

    /**
     * 
     *
     * @param c 
     * @return 
     */
    public HEM_Spherify setCenter(final WB_Point c) {
	sphere.setCenter(c);
	return this;
    }

    /**
     * 
     *
     * @param f 
     * @return 
     */
    public HEM_Spherify setFactor(final double f) {
	factor = f;
	return this;
    }

    /*
     * (non-Javadoc)
     * 
     * @see wblut.hemesh.modifiers.HEM_Modifier#apply(wblut.hemesh.core.HE_Mesh)
     */
    @Override
    public HE_Mesh apply(final HE_Mesh mesh) {
	final Iterator<HE_Vertex> vItr = mesh.vItr();
	HE_Vertex v;
	while (vItr.hasNext()) {
	    v = vItr.next();
	    v.set(gf.createInterpolatedPoint(v, sphere.projectToSphere(v),
		    factor));
	}
	return mesh;
    }

    /*
     * (non-Javadoc)
     * 
     * @seewblut.hemesh.modifiers.HEM_Modifier#applySelected(wblut.hemesh.core.
     * HE_Selection)
     */
    @Override
    public HE_Mesh apply(final HE_Selection selection) {
	final Iterator<HE_Vertex> vItr = selection.vItr();
	HE_Vertex v;
	while (vItr.hasNext()) {
	    v = vItr.next();
	    v.set(gf.createInterpolatedPoint(v, sphere.projectToSphere(v),
		    factor));
	}
	return selection.parent;
    }
}
